(function($, doc) {
    app.immersion(function() {
        doc.querySelector('header').classList.add('immersion');
    });

    $.init({
        preloadPages: [{
            id: 'native_pull',
            url: 'other/native_pull.html'
        }]
    });
    var account = $get('account');

    window.addEventListener('show', function() {
        var state = app.getState();
        account.innerText = state.account || 'admin';
    }, false);


    var settings = app.getSettings();
    var state = app.getState();

    $.plusReady(function() {
	    window.addEventListener('share_height_change', function (e) {
	    	shareweb.setStyle({
	    		height: e.detail.height + 'px'
	    	});
	    }, false);

        var self, olpage;
        var shareweb = $.preload({
        	id: 'share',
        	url: 'other/share.html',
        	styles: {
        		bottom: '0px',
        		background: 'transparent'
        	}
        });
        if ($.os.plus) {
            self = plus.webview.currentWebview();
            //mask click
	        self.parent().addEventListener("maskClick", function () {
		    	setTimeout(function () {
			        self.parent().setStyle({
			            mask: 'none'
			        });
		    	}, 200);
		        if (shareweb) {
		            shareweb.hide('slide-out-bottom', 200);
		        }
	        }, false);
            olpage = plus.webview.getWebviewById('outerlink');
        }

        $('#list1,#list2').on('tap', 'a', function(e) {
            var href = this.dataset.href;
            if (href) {
            	app.skip(this.innerText, href, href.replace('page/', ''));
            } else {
                switch (this.dataset.action) {
                    case 'native_pull':
                        $.openWindow({
                            id: 'native_pull',
                            url: 'other/native_pull.html'
                        });
                        break;
                    case 'immersion':
                        $.toast(plus.navigator.isImmersedStatusbar() || 'false');
                        break;
                    case 'native_ani':
                    	$.openWindow({
                    		id: 'native_ani',
                    		url: 'other/native_ani.html'
                    	});
                    	break;
                    case 'share':
                    	self.parent().setStyle({
                    		mask: 'rgba(0,0,0,0.5)'
                    	});
                    	shareweb.show('slide-in-bottom', 300);
                    	break;
                }
            }
        });
        // 九宫格区域
        $('#ngrid').on('tap', 'li', function() {
            var href = this.dataset.href;
            if (href) {
                $.fire(olpage, 'show', {
                    title: this.innerText,
                    url: encodeURIComponent(href)
                });
                $.openWindow({
                    id: 'outerlink',
                    url: href.replace('page/', '')
                });
            } else {
                switch (this.dataset.action) {
                    case 'email': // 实现main底部tab之间切换
                        $.fire(self.parent(), 'show', {
                            index: 2
                        });
                        break;
                    case 'phone':
                        $.fire(self.parent(), 'show', {
                            index: 1
                        });
                        break;
                }
            }
        });
    });
}(mui, document));